package com.powergisol.gis.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.powergisol.gis.dto.ExcelPileModelDTO;
import com.powergisol.gis.entity.*;
import com.powergisol.gis.util.XyChangeUtil;
import com.powergisol.gis.vo.ExcelInFailedDataVo;
import com.powergisol.gis.vo.TabPileVO;
import com.powergisol.gis.vo.TaskResultVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

/**
 * @author : laishihao
 * @date : 2020/10/22
 * description :
 */
public class PileModelListener extends AnalysisEventListener {

    private final static Pattern NUMBER_PATTERN = Pattern.compile("([+\\-]?[0-9]+[.]?[\\d]*)");
    //组装数据存放
    TabPileVO vo;
    //坐标系bean
    TabCoordinateSystem system;
    CacheManager cacheManager;

    String excelName;

    //字段值所在列数
    private Integer valueColumn;

    private ExcelPileModelDTO excelPileModelDTO;

    public PileModelListener() {

    }

    public PileModelListener(TabPileVO vo, TabCoordinateSystem system,
                             CacheManager cacheManager, ExcelPileModelDTO excelPileModelDTO) {
        this.vo = vo;
        this.system = system;
        this.cacheManager = cacheManager;
        this.excelPileModelDTO = excelPileModelDTO;


    }

    @Override
    public synchronized void invoke(Object o, AnalysisContext analysisContext) {
        ArrayList data = (ArrayList) o;
        try {
            if (data.isEmpty()) {
                return;
            }
            int j = 0;
            int dataSize = data.size();
            for (int i = 0; i < dataSize; i++) {
                if (j == 1) {
                    return;
                }
                String str = String.valueOf(data.get(i));
                if (StringUtils.isBlank(str)) continue;

                //如果列数存在
                if (null == valueColumn && str.equals("字段值")) {
                    valueColumn = i;
                }

                switch (str) {
                    case "Hcdm01":
                        vo.setHcdm01(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : String.valueOf(data.get(valueColumn)));
                        j++;
                        break;
                    case "Hcdm02":
                        vo.setHcdm02(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : String.valueOf(data.get(valueColumn)));
                        j++;
                        break;
                    case "Hcdm03":
                        vo.setHcdm03(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : String.valueOf(data.get(valueColumn)));
                        j++;
                        break;
                    case "Hcdm04":
                        vo.setHcdm04(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : String.valueOf(data.get(valueColumn)));
                        j++;
                        break;
                    case "Hcdm05":
                        vo.setHcdm05(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : String.valueOf(data.get(valueColumn)));
                        j++;
                        break;
                    case "Hcdm06":
                        vo.setHcdm06(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : String.valueOf(data.get(valueColumn)));
                        j++;
                        break;
                    case "Hcdm07":
                        vo.setHcdm07(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : String.valueOf(data.get(valueColumn)));
                        j++;
                        break;

                    case "GZ00":
                        vo.setGZ00(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : String.valueOf(data.get(valueColumn)));
                        j++;
                        break;
                    case "GZ01":
                        vo.setGZ01(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : String.valueOf(data.get(valueColumn)));
                        j++;
                        break;
                    case "GZ02":
                        vo.setGZ02(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : String.valueOf(data.get(valueColumn)));
                        j++;
                        break;
                    case "GZ03":
                        vo.setGZ03(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : String.valueOf(data.get(valueColumn)));
                        j++;
                        break;
                    case "GZ04":
                        vo.setGZ04(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : String.valueOf(data.get(valueColumn)));
                        j++;
                        break;
                    case "GZ05":
                        vo.setGZ05(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : String.valueOf(data.get(valueColumn)));
                        j++;
                        break;
                    case "GZ06":
                        vo.setGZ06(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : String.valueOf(data.get(valueColumn)));
                        j++;
                        break;
                    case "GZ07":
                        vo.setGZ07(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : String.valueOf(data.get(valueColumn)));
                        j++;
                        break;

                    case "GZ21":
                        vo.setGZ21(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : Double.parseDouble(data.get(valueColumn).toString()));
                        j++;
                        break;
                    case "GZ22":
                        vo.setGZ22(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : Double.parseDouble(data.get(valueColumn).toString()));
                        j++;
                        break;
                    case "GZ23":
                        vo.setGZ23(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : Double.parseDouble(data.get(valueColumn).toString()));
                        j++;
                        break;
                    case "GZ24":
                        vo.setGZ24(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : Double.parseDouble(data.get(valueColumn).toString()));
                        j++;
                        break;
                    case "GZ25":
                        vo.setGZ25(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : Double.parseDouble(data.get(valueColumn).toString()));
                        j++;
                        break;
                    case "GZ26":
                        vo.setGZ26(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : Double.parseDouble(data.get(valueColumn).toString()));
                        j++;
                        break;
                    case "GZ27":
                        vo.setGZ27(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : data.get(valueColumn).toString());
                        j++;
                        break;
                    case "GZ28":
                        vo.setGZ28(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : String.valueOf(data.get(valueColumn)));
                        j++;
                        break;
                    case "GZ29":
                        vo.setGZ29(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : String.valueOf(data.get(valueColumn)));
                        j++;
                        break;
                    case "GZ30":
                        vo.setGZ30(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : String.valueOf(data.get(valueColumn)));
                        j++;
                        break;
                    case "GZ31":
                        vo.setGZ31(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : String.valueOf(data.get(valueColumn)));
                        j++;
                        break;
                    case "GZ32":
                        vo.setGZ32(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : String.valueOf(data.get(valueColumn)));
                        j++;
                        break;

                    case "GZ41":
                        vo.setGZ41(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : String.valueOf(data.get(valueColumn)));
                        j++;
                        break;
                    case "GZ42":
                        vo.setGZ42(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : String.valueOf(data.get(valueColumn)));
                        j++;
                        break;
                    case "GZ43":
                        vo.setGZ43(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : String.valueOf(data.get(valueColumn)));
                        j++;
                        break;

                    case "GZ51":
                        vo.setGZ51(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : String.valueOf(data.get(valueColumn)));
                        j++;
                    case "GZZ（1，i）":
                        if (valueColumn <= dataSize) {
                            List<String> GZZ1 = new ArrayList<>();
                            for (int numj = valueColumn; numj < data.size(); numj++) {
                                GZZ1.add(null == data.get(numj) ? null : data.get(numj).toString());
                            }
                            vo.setGZZ1List(GZZ1);
                        }
                        j++;
                        break;
                    case "GZZ（2，i）":
                        if (valueColumn <= dataSize) {
                            List<String> GZZ2 = new ArrayList<>();
                            for (int numj = valueColumn; numj < data.size(); numj++) {
                                GZZ2.add(null == data.get(numj) ? null : data.get(numj).toString());
                            }
                            vo.setGZZ2List(GZZ2);
                        }
                        j++;
                        break;
                    case "GZZ（3，i）":
                        if (valueColumn <= dataSize) {
                            List<String> GZZ3 = new ArrayList<>();
                            for (int numj = valueColumn; numj < data.size(); numj++) {
                                GZZ3.add(null == data.get(numj) ? null : data.get(numj).toString());
                            }
                            vo.setGZZ3List(GZZ3);
                        }
                        j++;
                        break;
                    case "GZZ（4，i）":
                        if (valueColumn <= dataSize) {
                            List<Integer> GZZ4 = new ArrayList<>();
                            for (int numj = valueColumn; numj < data.size(); numj++) {
                                GZZ4.add(null == data.get(numj) ? null : Integer.parseInt(data.get(numj).toString()));
                            }
                            vo.setGZZ4List(GZZ4);
                        }
                        j++;
                        break;

                    case "GZ61":
                        vo.setGZ61(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : String.valueOf(data.get(valueColumn)));
                        j++;
                        break;
                    case "GZ62":
                        vo.setGZ62(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : String.valueOf(data.get(valueColumn)));
                        j++;
                        break;
                    case "GZ63":
                        vo.setGZ63(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : String.valueOf(data.get(valueColumn)));
                        j++;
                        break;
                    case "GZ64":
                        vo.setGZ64(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : String.valueOf(data.get(valueColumn)));
                        j++;
                        break;

                    case "GZ65":
                        vo.setGZ65(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : String.valueOf(data.get(valueColumn)));
                        j++;
                        break;
                    case "GZ66":
                        vo.setGZ66(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : String.valueOf(data.get(valueColumn)));
                        j++;
                        break;
                    case "GZ67":
                        vo.setGZ67(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : String.valueOf(data.get(valueColumn)));
                        j++;
                        break;

                    case "GZ68":
                        vo.setGZ68(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : String.valueOf(data.get(valueColumn)));
                        j++;
                        break;
                    case "GZ69":
                        vo.setGZ69(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : String.valueOf(data.get(valueColumn)));
                        j++;
                        break;
                    case "GZ70":
                        vo.setGZ70(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : String.valueOf(data.get(valueColumn)));
                        j++;
                        break;
                    case "GZ71":
                        vo.setGZ71(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : Integer.parseInt(String.valueOf(data.get(valueColumn))));
                        j++;
                        break;
                    case "GZ72":
                        vo.setGZ72(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : Integer.parseInt(String.valueOf(data.get(valueColumn))));
                        j++;
                        break;
                    case "GZ73":
                        vo.setGZ73(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : Integer.parseInt(String.valueOf(data.get(valueColumn))));
                        j++;
                        break;

                    case "GS01":
                        vo.setGS01(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : String.valueOf(data.get(valueColumn)));
                        j++;
                        break;
                    case "GS02":
                        vo.setGS02(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : String.valueOf(data.get(valueColumn)));
                        j++;
                        break;
                    case "GS03":
                        vo.setGS03(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : String.valueOf(data.get(valueColumn)));
                        j++;
                        break;
                    case "GS04":
                        vo.setGS04(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : String.valueOf(data.get(valueColumn)));
                        j++;
                        break;
                    case "GS05":
                        vo.setGS05(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : String.valueOf(data.get(valueColumn)));
                        j++;
                        break;
                    case "GS06":
                        vo.setGS06(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : String.valueOf(data.get(valueColumn)));
                        j++;
                        break;
                    case "GS07":
                        vo.setGS07(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : String.valueOf(data.get(valueColumn)));
                        j++;
                        break;

                    case "GS11":
                        vo.setGS11(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : String.valueOf(data.get(valueColumn)));
                        j++;
                        break;
                    case "GS12":
                        vo.setGS12(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : String.valueOf(data.get(valueColumn)));
                        j++;
                        break;
                    case "GS13":
                        vo.setGS13(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : String.valueOf(data.get(valueColumn)));
                        j++;
                        break;
                    case "GS14":
                        vo.setGS14(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : String.valueOf(data.get(valueColumn)));
                        j++;
                        break;
                    case "GS15":
                        vo.setGS15(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : String.valueOf(data.get(valueColumn)));
                        j++;
                        break;
                    case "GS16":
                        vo.setGS16(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : String.valueOf(data.get(valueColumn)));
                        j++;
                        break;
                    case "GS17":
                        vo.setGS17(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : String.valueOf(data.get(valueColumn)));
                        j++;
                        break;
                    case "GS18":
                        vo.setGS18(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : String.valueOf(data.get(valueColumn)));
                        j++;
                        break;
                    case "GS19":
                        vo.setGS19(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : String.valueOf(data.get(valueColumn)));
                        j++;
                        break;
                    case "GS20":
                        vo.setGS20(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : String.valueOf(data.get(valueColumn)));
                        j++;
                        break;
                    case "GS23":
                        vo.setGS23(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : String.valueOf(data.get(valueColumn)));
                        j++;
                        break;
                    default:
                        break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        saveDate(vo);
    }

    private void saveDate(TabPileVO vo) {
        try {
            List<ExcelInFailedDataVo> listFailed = new ArrayList<>();//失败数据
            BigDecimal lat = BigDecimal.ZERO;
            BigDecimal lon = BigDecimal.ZERO;
            if (!NUMBER_PATTERN.matcher(String.valueOf(vo.getGZ21())).matches()) {
                dealFail("坐标Xl需为数字");
                return;
            }
            if (!NUMBER_PATTERN.matcher(String.valueOf(vo.getGZ22())).matches()) {
                dealFail("坐标Yw需为数字");
                return;
            }

            //设置桩的 原始坐标
            vo.setOGZ21(vo.getGZ21());
            vo.setOGZ22(vo.getGZ22());


            if (1 == vo.getIsChange()) {
                lat = XyChangeUtil.change(vo.getGZ21(), vo.getGZ22(), system.getCoorChangeLat());
                lon = XyChangeUtil.change(vo.getGZ21(), vo.getGZ22(), system.getCoorChangeLon());
            } else {
                lat = new BigDecimal(vo.getGZ21());//纬度
                lon = new BigDecimal(vo.getGZ22());//经度
            }

            if (lon.compareTo(new BigDecimal("180")) > 0 || lon.compareTo(new BigDecimal("-180")) < 0 ||
                    lat.compareTo(new BigDecimal("90")) > 0 || lat.compareTo(new BigDecimal("-90")) < 0) {
                dealFail("坐标Xl或坐标Yw不在范围内");
                return;
            }

            //保存更改坐标系后经纬度 或原始正确经纬度
            vo.setGZ21(lon.doubleValue());
            vo.setGZ22(lat.doubleValue());
            //桩：包含桩的CDM标准、基本信息、坐标及表面几何参数和其他参数
//            excelPileModelDTO.getPileVOList().add(vo);
            TabPile pile = new TabPile();
            pile.setUuid(vo.getUuid());
            pile.setHcdm01(vo.getHcdm01());
            pile.setHcdm02(vo.getHcdm02());
            pile.setHcdm03(vo.getHcdm03());
            pile.setHcdm04(vo.getHcdm04());
            pile.setHcdm05(vo.getHcdm05());
            pile.setHcdm06(vo.getHcdm06());
            pile.setHcdm07(vo.getHcdm07());
            pile.setGZ00(vo.getGZ00());
            pile.setGZ01(vo.getGZ01());
            pile.setGZ02(vo.getGZ02());
            pile.setGZ03(vo.getGZ03());
            pile.setGZ04(vo.getGZ04());
            pile.setGZ05(vo.getGZ05());
            pile.setGZ06(vo.getGZ06());
            pile.setGZ07(vo.getGZ07());
            pile.setGZ21(vo.getGZ21());
            pile.setOGZ21(vo.getOGZ21());
            pile.setGZ22(vo.getGZ22());
            pile.setOGZ22(vo.getOGZ22());
            pile.setGZ23(vo.getGZ23());
            pile.setGZ24(vo.getGZ24());
            pile.setGZ25(vo.getGZ25());
            pile.setGZ26(vo.getGZ26());
            pile.setGZ27(vo.getGZ27());
            pile.setGZ28(vo.getGZ28());
            pile.setGZ29(vo.getGZ29());
            pile.setGZ30(vo.getGZ30());
            pile.setGZ31(vo.getGZ31());
            pile.setGZ32(vo.getGZ32());
            pile.setGZ41(vo.getGZ41());
            pile.setGZ42(vo.getGZ42());
            pile.setGZ43(vo.getGZ43());
            pile.setGZ51(vo.getGZ51());
            excelPileModelDTO.getPileList().offer(pile);
//            pileMapper.insertSelective(vo);

            //桩的其他参数
            TabPileOther other = new TabPileOther();
            other.setUuid(vo.getUuid());
            other.setHcdm01(vo.getHcdm01());
            other.setGS01(vo.getGS01());
            other.setGS02(vo.getGS02());
            other.setGS03(vo.getGS03());
            other.setGS04(vo.getGS04());
            other.setGS05(vo.getGS05());
            other.setGS06(vo.getGS06());
            other.setGS07(vo.getGS07());
            other.setGS11(vo.getGS11());
            other.setGS12(vo.getGS12());
            other.setGS13(vo.getGS13());
            other.setGS14(vo.getGS14());
            other.setGS15(vo.getGS15());
            other.setGS16(vo.getGS16());
            other.setGS17(vo.getGS17());
            other.setGS18(vo.getGS18());
            other.setGS19(vo.getGS19());
            other.setGS20(vo.getGS20());
            other.setGS23(vo.getGS23());
            excelPileModelDTO.getPileOtherList().offer(other);
//            OtherMapper.insertSelective(other);

            //桩对应的配筋段数
            Integer reinNumber = Integer.valueOf(vo.getGZ51());
            //桩对应的配筋基础信息
            for (int i = 0; i < reinNumber; i++) {
                TabPileReinforcement reinforcement = new TabPileReinforcement();
                reinforcement.setGZZ1(vo.getGZZ1List().get(i));
                reinforcement.setGZZ2(vo.getGZZ2List().get(i));
                reinforcement.setGZZ3(vo.getGZZ3List().get(i));
                if(null != vo.getGZZ4List()){
                    reinforcement.setGZZ4(vo.getGZZ4List().get(i));
                }
                reinforcement.setUuid(vo.getUuid());
                reinforcement.setHcdm01(vo.getHcdm01());

//                reinforcementMapper.insertSelective(reinforcement);
                excelPileModelDTO.getReinforcementList().offer(reinforcement);
            }

            //桩对应的配筋 - 额外信息表
            TabPileReinforcementDetails details = new TabPileReinforcementDetails();
            details.setUuid(vo.getUuid());
            details.setHcdm01(vo.getHcdm01());
            details.setGZ61(vo.getGZ61());
            details.setGZ62(vo.getGZ62());
            details.setGZ63(vo.getGZ63());
            details.setGZ64(vo.getGZ64());
            details.setGZ65(vo.getGZ65());
            details.setGZ66(vo.getGZ66());
            details.setGZ67(vo.getGZ67());
            details.setGZ68(vo.getGZ68());
            details.setGZ69(vo.getGZ69());
            details.setGZ70(vo.getGZ70());
            details.setGZ71(vo.getGZ71());
            details.setGZ72(vo.getGZ72());
            details.setGZ73(vo.getGZ73());
//            detailsMapper.insertSelective(details);
            excelPileModelDTO.getDetailsList().offer(details);

           // if (excelCount == excelTotal) {
                //桩的基本信息集合
               // pileMapper.insertSelectiveList(pileVOList);
                //桩的其他参数
//              //桩对应的配筋基础信息集合
//                reinforcementMapper.insertSelectiveList(reinforcementList);
//              //桩对应的配筋 - 额外信息表集合
//                detailsMapper.insertSelectiveList(detailsList);
        } catch (Exception e) {
            e.printStackTrace();
        }


    }


    private void dealFail(String msg) {
        List<ExcelInFailedDataVo> listFailed = new ArrayList<>();
        ExcelInFailedDataVo failedDataVo = new ExcelInFailedDataVo();
        failedDataVo.setReason(msg);
        failedDataVo.setRowIndex(excelName);
        listFailed.add(failedDataVo);
        Cache cache = cacheManager.getCache("cache-data");
        TaskResultVo resultVo = new TaskResultVo();
        resultVo.setListFailed(listFailed);
        cache.put(excelName + "sb", resultVo);
    }

    private boolean isNumber(String value) {
        String matchStr = "^\\d+$";
        return value.matches(matchStr);
    }

}
